package com.kaifamiao.kfm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaifamiao.kfm.entity.SysMenu;
import com.kaifamiao.kfm.entity.SysRole;
import com.kaifamiao.kfm.entity.SysUser;
import com.kaifamiao.kfm.entity.TreeMenuDTO;
import com.kaifamiao.kfm.mapper.SysUserMapper;
import com.kaifamiao.kfm.service.SysMenuService;
import com.kaifamiao.kfm.service.SysRoleService;
import com.kaifamiao.kfm.service.SysUserService;
import com.kaifamiao.kfm.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.print.DocFlavor;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* @author 10854
* @description 针对表【sys_user(用户信息表)】的数据库操作Service实现
* @createDate 2022-08-21 11:30:27
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService, UserDetailsService {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public Collection<TreeMenuDTO> getMenus(SysUser user) {
        if (user != null){
            List<SysRole> roles = sysRoleService.getRolesByUserId(user.getUserId());
            Set<SysMenu> menus = new HashSet<>();
            for (SysRole role : roles) {
                List<SysMenu> list = null;
                if (role.getRoleKey().equals("admin")){
                    list = sysMenuService.list();
                } else {
                    list = sysMenuService.findByRoleId(role.getRoleId());
                }
                menus.addAll(list);
            }

            return sysMenuService.treeMenu(menus);
        }
        return null;
    }

    @Override
    public Map<String, List> grantData(Long id) {
        List<SysRole> all = sysRoleService.list();
        List<Long> grant = sysRoleService.getRoleIdByUserId(id);
        Map<String, List> result = new HashMap<>();
        result.put("all", all);
        result.put("grant", grant);
        return result;
    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("email", username);
        SysUser user = getOne(wrapper);

        if (user != null){
            Set<GrantedAuthority> set = new HashSet<>();
            List<SysRole> roles = sysRoleService.getRolesByUserId(user.getUserId());

            for (SysRole role : roles){
                List<SysMenu> menus = sysMenuService.findByRoleId(role.getRoleId());
                // 角色权限
                set.add(new SimpleGrantedAuthority(Constants.ROLE_PRE + role.getRoleKey()));

                //
//                Set<SysMenu> set1 = new HashSet<>();
//                for (SysMenu menu : menus){
//                    if (StringUtils.hasLength(menu.getPerms())){
//                        set1.add(menu);
//                    }
//                }

                Set<SysMenu> menus1 = menus.stream()
                        .filter((SysMenu e) -> StringUtils.hasLength(e.getPerms()))
                        .collect(Collectors.toSet());
                // 菜单权限
                for (SysMenu menu : menus1){
                    set.add(new SimpleGrantedAuthority(menu.getPerms()));
                }

            }

            // 将权限放入到 UserDetails 对象中
            user.setAuthorities(set);
        }
        return user;
    }
}




